/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Barang.java
 *
 * Created on 30 Nov 10, 10:58:09
 */
package view;

import controller.ControllerBarang;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.border.BevelBorder;
import utility.TableGenerator;

/**
 *
 * @author Mirza
 */
public class Barang extends javax.swing.JPanel {

    private int selectedIndex = 0;
    private boolean tambahBarang = false;
    Paint paint;
    /** Creates new form Barang */
    public Barang() {
        initComponents();
        refreshTabel();
        setActiveInput(false);
        setOpaque(false);
        setBorder(new BevelBorder(BevelBorder.LOWERED));
        jButtonHapus.setVisible(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D gd = (Graphics2D) g.create();
        paint = new GradientPaint(getWidth(), 0, Color.BLUE, 0, 0, Color.WHITE);
        gd.setPaint(paint);
        gd.fillRect(0, 0, getWidth(), getHeight());
        gd.dispose();
        super.paintComponent(g);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableBarang = new javax.swing.JTable();
        jTextFieldPencarian = new javax.swing.JTextField();
        jComboBoxJenisPencarian = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNamaBarang = new javax.swing.JTextField();
        jTextFieldStok = new javax.swing.JTextField();
        jTextFieldBarcode = new javax.swing.JTextField();
        jButtonBatal = new javax.swing.JButton();
        jButtonSimpan = new javax.swing.JButton();
        jButtonHapus = new javax.swing.JButton();
        jButtonUbah = new javax.swing.JButton();
        jButtonTambah = new javax.swing.JButton();
        jButton_pilhBarang = new javax.swing.JButton();

        jTableBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "KODE BARANG", "NAMA BARANG", "STOK", "BARCODE"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableBarang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableBarangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableBarang);
        jTableBarang.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTableBarang.getColumnModel().getColumn(1).setPreferredWidth(10);
        jTableBarang.getColumnModel().getColumn(2).setPreferredWidth(20);

        jTextFieldPencarian.setText("search");
        jTextFieldPencarian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPencarianActionPerformed(evt);
            }
        });
        jTextFieldPencarian.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldPencarianFocusGained(evt);
            }
        });

        jComboBoxJenisPencarian.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nama Barang", "Stok", "Barcode" }));

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel1.setText("Nama Barang");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel2.setText("Stok");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel3.setText("Barcode");

        jButtonBatal.setText("Batal");
        jButtonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBatalActionPerformed(evt);
            }
        });

        jButtonSimpan.setText("Simpan");
        jButtonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSimpanActionPerformed(evt);
            }
        });

        jButtonHapus.setText("Hapus");
        jButtonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHapusActionPerformed(evt);
            }
        });

        jButtonUbah.setText("Ubah");
        jButtonUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUbahActionPerformed(evt);
            }
        });

        jButtonTambah.setText("Tambah");
        jButtonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTambahActionPerformed(evt);
            }
        });

        jButton_pilhBarang.setText("Pilih");
        jButton_pilhBarang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pilhBarangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldStok, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                            .addComponent(jTextFieldNamaBarang, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButtonBatal, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldBarcode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonTambah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonUbah, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonHapus, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 259, Short.MAX_VALUE)
                        .addComponent(jButton_pilhBarang, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(126, 126, 126)
                        .addComponent(jButtonSimpan, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jComboBoxJenisPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxJenisPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonUbah)
                    .addComponent(jButtonTambah)
                    .addComponent(jButtonHapus)
                    .addComponent(jButton_pilhBarang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldNamaBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldStok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldBarcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonBatal)
                    .addComponent(jButtonSimpan))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void refreshTabel() {
        try {
            String[] listField = {"KODE_BARANG", "NAMA_BARANG", "STOK", "BARCODE"};
            TableGenerator.generateTable(jTableBarang, ControllerBarang.searchBarang(""), listField);

        } catch (SQLException ex) {
            Logger.getLogger(Suplier.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void kosongkanInput() {
        jTextFieldNamaBarang.setText("");
        jTextFieldStok.setText("");
        jTextFieldBarcode.setText("");
    }

    private void setActiveInput(boolean status) {
        jButtonTambah.setEnabled(!status);
        jButtonUbah.setEnabled(!status);
        jButtonHapus.setEnabled(!status);
        jTextFieldNamaBarang.setEnabled(status);
        jTextFieldStok.setEnabled(status);
        jTextFieldBarcode.setEnabled(status);
        jButtonSimpan.setEnabled(status);
        jButtonBatal.setEnabled(status);
    }

    private void tampilkanDataTabelKeInput() {
        jTextFieldNamaBarang.setText((String) jTableBarang.getValueAt(selectedIndex, 1));
        jTextFieldStok.setText((String) jTableBarang.getValueAt(selectedIndex, 2));
        jTextFieldBarcode.setText((String) jTableBarang.getValueAt(selectedIndex, 3));
    }

    private void jButtonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTambahActionPerformed
        // TODO add your handling code here:
        kosongkanInput();
        tambahBarang = true;
        setActiveInput(true);
    }//GEN-LAST:event_jButtonTambahActionPerformed

    private void jButtonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSimpanActionPerformed
        // TODO add your handling code here:
        if (tambahBarang) {
            if (ControllerBarang.insertIntoBarang(jTextFieldNamaBarang.getText(), jTextFieldStok.getText(), jTextFieldBarcode.getText())) {
                JOptionPane.showMessageDialog(this, "Data Berhasil dimasukkan");
                refreshTabel();
            }
        } else {
            if (ControllerBarang.updateBarang((String) jTableBarang.getValueAt(selectedIndex, 0), jTextFieldNamaBarang.getText(), jTextFieldStok.getText(), jTextFieldBarcode.getText())) {
                JOptionPane.showMessageDialog(this, "Data Berhasil diubah");
                refreshTabel();
            }
        }
        setActiveInput(false);
}//GEN-LAST:event_jButtonSimpanActionPerformed

    private void jTextFieldPencarianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPencarianActionPerformed
        try {
            // TODO add your handling code here:
            String[] listField = {"KODE_BARANG", "NAMA_BARANG", "STOK", "BARCODE"};
            String filter = "";
            if (!jTextFieldPencarian.getText().equals("")) {
                filter = "where " + jComboBoxJenisPencarian.getItemAt(jComboBoxJenisPencarian.getSelectedIndex()).toString().replaceAll(" ", "_") + " like '%" + jTextFieldPencarian.getText() + "%'";
            }

            TableGenerator.generateTable(jTableBarang, ControllerBarang.searchBarang(filter), listField);
        } catch (SQLException ex) {
            Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTextFieldPencarianActionPerformed

    private void jButtonUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUbahActionPerformed
        // TODO add your handling code here:
        tambahBarang = false;
        setActiveInput(true);
    }//GEN-LAST:event_jButtonUbahActionPerformed

    private void jButtonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHapusActionPerformed
        // TODO add your handling code here:
        int a = JOptionPane.showConfirmDialog(this, "Apakah anda yakin akan menghapus data ?");
        if (a == 0) {
            ControllerBarang.deleteFromBarang((String) jTableBarang.getValueAt(selectedIndex, 0));
            refreshTabel();
        }
    }//GEN-LAST:event_jButtonHapusActionPerformed

    private void jButtonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBatalActionPerformed
        // TODO add your handling code here:
        kosongkanInput();
        tambahBarang = true;
        setActiveInput(false);
    }//GEN-LAST:event_jButtonBatalActionPerformed

    private void jTableBarangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableBarangMouseClicked
        selectedIndex = jTableBarang.getSelectedRow();
        tampilkanDataTabelKeInput();
        tambahBarang = false;
    }//GEN-LAST:event_jTableBarangMouseClicked

    private void jButton_pilhBarangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pilhBarangActionPerformed
        ViewUtama.kodeBarang = (String) jTableBarang.getValueAt(selectedIndex, 0);
        ViewUtama.formBarang.setVisible(false);
}//GEN-LAST:event_jButton_pilhBarangActionPerformed

    private void jTextFieldPencarianFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldPencarianFocusGained
        jTextFieldPencarian.setText("");
    }//GEN-LAST:event_jTextFieldPencarianFocusGained
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBatal;
    private javax.swing.JButton jButtonHapus;
    private javax.swing.JButton jButtonSimpan;
    private javax.swing.JButton jButtonTambah;
    private javax.swing.JButton jButtonUbah;
    private javax.swing.JButton jButton_pilhBarang;
    private javax.swing.JComboBox jComboBoxJenisPencarian;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableBarang;
    private javax.swing.JTextField jTextFieldBarcode;
    private javax.swing.JTextField jTextFieldNamaBarang;
    private javax.swing.JTextField jTextFieldPencarian;
    private javax.swing.JTextField jTextFieldStok;
    // End of variables declaration//GEN-END:variables
}
